package com.lanou3g.ptp.task.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou3g.ptp.achievement.service.DiaryService;
import com.lanou3g.ptp.achievement.service.ReportService;
import com.lanou3g.ptp.achievement.service.SummaryService;
import com.lanou3g.ptp.entity.*;
import com.lanou3g.ptp.exception.PtpException;
import com.lanou3g.ptp.task.mapper.ExcellentMapper;
import com.lanou3g.ptp.task.mapper.TaskMapper;
import com.lanou3g.ptp.task.service.ExcellentService;
import com.lanou3g.ptp.utils.DateUtils;
import com.lanou3g.ptp.utils.RegexUtils;
import com.lanou3g.ptp.vo.ExcellentBody;
import com.lanou3g.ptp.vo.ExcellentParams;
import com.lanou3g.ptp.vo.Option;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 创建人: 武奇
 * 创建事件: 2019/8/29
 */
@Service(interfaceClass = ExcellentService.class, timeout = 5000)
@Slf4j
public class ExcellentServiceImpl implements ExcellentService {

    @Autowired
    private ExcellentMapper excellentMapper;

    @Reference
    private DiaryService diaryService;

    @Reference
    private ReportService reportService;

    @Reference
    private SummaryService summaryService;


    @Autowired
    private TaskMapper taskMapper;

    @Transactional
    @Override
    public void diaryExcellent(Long diaryId) throws PtpException {
        Date now = DateUtils.now();
        Diary diary = this.diaryService.findDiaryById(diaryId);
        if (diary.getExcellent() != null && diary.getExcellent()) {
            throw new PtpException(400, "已经被推优过了");
        }
        Excellent ecl = Excellent.builder()
                .studentId(diary.getStudentId())
                .taskId(diary.getTaskId())
                .content(diary.getContent())
                .img(diary.getImg())
                .type(0)// 0表示日志
                .createTime(now)
                .updateTime(now)
                .status(0)
                .build();
        this.excellentMapper.insertOne(ecl);
        // 把日志的标记更新为已被推优
        this.diaryService.excellentDiary(diary.getId());
    }

    @Transactional
    @Override
    public void reportExcellent(Long reportId) throws PtpException {
        Date now = DateUtils.now();
        Report report = this.reportService.findById(reportId);
        if (report.getExcellent() != null && report.getExcellent()) {
            throw new PtpException("阶段报告已被推优");
        }
        String content = report.getContent();
        String[] imgs = RegexUtils.findImgUrlsInHtml(content);
        Excellent ecl = Excellent.builder()
                .studentId(report.getStudentId())
                .taskId(report.getTaskId())
                .content(content)
                .img(String.join(",", imgs))
                .type(1)
                .createTime(now)
                .updateTime(now)
                .status(0)
                .build();
        this.excellentMapper.insertOne(ecl);
        this.reportService.excellentReport(report.getId());


    }

    @Transactional
    @Override
    public void summaryExcellent(Long summaryId) throws PtpException {
        Date now = DateUtils.now();
        Summary summary = this.summaryService.findSummaryById(summaryId);
        if (summary.getExcellent() != null && summary.getExcellent()) {
            throw new PtpException("总结已被推优");
        }
        String content = summary.getContent();
        String[] imgs = RegexUtils.findImgUrlsInHtml(content);
        Excellent ecl = Excellent.builder()
                .studentId(summary.getStudentId())
                .taskId(summary.getTaskId())
                .content(content)
                .img(String.join(",", imgs))
                .type(1)
                .createTime(now)
                .updateTime(now)
                .status(0)
                .build();
        this.excellentMapper.insertOne(ecl);
        this.summaryService.excellentReport(summary.getId());

    }

    @Override
    public PageInfo<ExcellentBody> getExcellent(Teacher teacher, ExcellentParams params) {
        List<Long> taskIds = null;
        // 判断当前老师是学院老师还是专业老师
        // 学院老师没有专业id
        if (teacher.getMajorId() == null) {
            // 学院老师
            taskIds = this.taskMapper.findTaskIdByCollegeTeacher(teacher.getId());
        } else {
            // 专业老师
            taskIds = this.taskMapper.findTaskIdByMajorTeacher(teacher.getId());
        }
        PageHelper.startPage(params.getPage(), params.getSize());

        List<ExcellentBody> excellent = this.excellentMapper.findExcellent(params, taskIds);
        return PageInfo.of(excellent);
    }

    @Override
    public List<Option> optionGrades() {
        return this.excellentMapper.optionGrades();
    }
}
